package com.sgb.kjwl.viewmodel.home;

import android.content.Intent;

import com.chuanglan.shanyan_sdk.OneKeyLoginManager;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.LoginEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.model.entity.user.OneKeyLoginEntity;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.self.userInfo.SetPwdActivity;
import com.swgk.core.mmkvutil.CacheMarker;
import com.sgb.kjwl.view.ui.fragment.home.BigHomeVideoFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.lang.reflect.Type;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

public class BigHomeVideoFragModel extends BaseViewModel {
    private HomeRepertory repertory;
    private BigHomeVideoFragment fragment;

    public BigHomeVideoFragModel(BigHomeVideoFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;

    }

    public void getVideoListByType(String id) {
        //DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        Type type = new TypeToken<List<VideoBean>>() {}.getType();
        Object obj = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.CacheVideoListData + id,type);
        if (null != obj) {
            List<VideoBean> videoList = (List<VideoBean>)obj;
            fragment.showList5(videoList);
        }
        APIManager.getInstance().getBigHomeAPI().getVideoListByType(id,1,3).enqueue(new ICallBack<BaseEntity<ProListEntity<List<VideoBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<VideoBean>>> data) {
                if (data == null) {
                    return;
                }
                boolean isEqual = DatalUtils.getInstance().IsCahceEqual(data.getData(), CacheMarker.CacheVideoListData + id);
                if (!isEqual) {
                    fragment.showList5(data.getData().getList());
                }
            }
        });
    }

    public void oneKeyLogin(OneKeyLoginEntity oneKeyLoginEntity) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "登录中", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().oneClickLogin(oneKeyLoginEntity).enqueue(new ICallBack<BaseEntity<LoginEntity>>() {
            @Override
            public void onSuccess(BaseEntity<LoginEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    OneKeyLoginManager.getInstance().finishAuthActivity();
                    if (data.getData().isNeedRegister()) {
                        Intent intent = new Intent(fragment.getActivity(), SetPwdActivity.class);
                        intent.putExtra("mobile", data.getData().getPhone());
                        fragment.getActivity().startActivity(intent);
                    } else {
                        //登录成功逻辑
                        saveUserInfo(data.getData());
                        fragment.loginSucceed(data.getData(), data.getData().getPhone());
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, data.getData().getToken());
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, data.getData().getPhone());
                        JPushInterface.setAlias(fragment.getActivity(), 1, data.getData().getPhone());
                        getUserInfo();
                    }
                }
            }
        });
    }

    public void getUserInfo() {
//        DialogHelper.showProgressDialog(fragment.getActivity(), null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new ICallBack<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UserMsgEntity> data) {
                if (data == null) return;
                if (data.getData() == null) return;
                if (data.getState().equals("ok")) {
                    fragment.getUserMsgSucceed(data.getData());
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

    private void saveUserInfo(LoginEntity loginEntity) {
        repertory.saveToken(loginEntity.getToken());
        repertory.saveAccount(loginEntity.getPhone());
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("competitivePurchase").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
//                    String url = "http://192.168.2.33:8067/CompetitivePurchase" + "?id=" + id;
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }
}
